/*
 * Copyright 2014 Adem Vural <a.vural@cibek.de>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.crossbone.admin;

import com.crossbone.dao.UserDao;
import com.crossbone.model.User;
import com.crossbone.spring.Adder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.CheckBoxMultipleChoice;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author Adem Vural <adem.vural@stud.hs-mannheim.de>
 */
public class CheckBoxMultipleChoicePage extends WebPage {

    @SpringBean
    Adder adder;

    @SpringBean
    UserDao dao;

    private static final List<String> LANGUAGES = Arrays.asList(new String[]{
        "Java", ".NET", "PHP", "Ruby", "C/C++"
    });

    private static final List<String> HOSTING_TYPES = Arrays.asList(new String[]{
        "Shared Host", "VPS", "Clound Host", "Dedicated Server"
    });
//___
    private final List<User> USERS = (List<User>) dao.getUserList();

    private ArrayList<User> userSelect = new ArrayList<User>();

//___
    //holds checkbox values
    private ArrayList<String> languagesSelect = new ArrayList<String>();

    //checked vps and dedicated server by default
    private ArrayList<String> hostingtSelect = new ArrayList<String>(
            Arrays.asList(new String[]{"VPS", "Dedicated Server"}));

    //Dropdown 
    private static final List<String> USERBASE = Arrays.asList(new String[]{
        "Benutzerliste", "Bewohnerliste"});
    //make Benutzerliste selected by default
    private String selected = "Benutzerliste";

    public CheckBoxMultipleChoicePage(final PageParameters parameters) {
        add(new FeedbackPanel("feedback"));

        final CheckBoxMultipleChoice<String> listLanguages
                = new CheckBoxMultipleChoice<String>(
                        "languages", new Model(languagesSelect), LANGUAGES);

        final CheckBoxMultipleChoice<String> listHosting
                = new CheckBoxMultipleChoice<String>(
                        "hostings", new Model(hostingtSelect), HOSTING_TYPES);
        //__
        final CheckBoxMultipleChoice<User> listUser = new CheckBoxMultipleChoice<User>("users", new Model(userSelect), USERS);
//__

        /*
         Checkbox example
         */
        final User u = (User) dao.getUserList().get(0);

        final CheckBox chk0 = new CheckBox("checkbox", Model.of(u.isActive()));

//Dropdown
        DropDownChoice<String> userbase = new DropDownChoice<String>("userbase", new PropertyModel<String>(this, "selected"), USERBASE);
        
        
        Form<?> form = new Form<Void>("userForm") {
            @Override
            protected void onSubmit() {
                info("Languages: " + languagesSelect.toString());
                info("Hosting Types: " + hostingtSelect.toString());
                info("User: " + userSelect.toString());
                info("User: list " + USERS.size());

                //__ CheckBox
                u.setActive(chk0.getModelObject());
                info("Ist user noch aktiv ? " + u.isActive());
                dao.updateUser(u);

                //dropbox
                info("Selected USERBASE : "+selected);
            }
        };

        
        
        add(form);
        form.add(listLanguages);
        form.add(listHosting);
        form.add(listUser);

        form.add(chk0);
        form.add(userbase);
    }

}
